<?php
/* SVN FILE: $Id: templateItem.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket Item template/base class
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/sources/templateItem.php $
*/

abstract class templateItem
{
	/**
	 * The item information that was stored in database
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $item;
	
	// Default Item Properties
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName;
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription;
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = false;
	
	/**
	 * Item setting telling that tell what is needed
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $dependcies = array(
									'item'		=> array(),
									'plugin'	=> array(),
									'server'	=> array(),
								);
	
	/**#@+
	 * Registry Object Shortcuts
	 *
	 * @access	protected
	 * @var		object
	 */
	protected $registry;
	protected $DB;
	protected $settings;
	protected $request;
	protected $lang;
	protected $member;
	protected $memberData;
	protected $cache;
	protected $caches;
	/**#@-*/
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		$this->item = &$item;
		
		/* Make registry objects */
		$this->registry   =  $registry;
		$this->DB         =  $this->registry->DB();
		$this->settings   =& $this->registry->fetchSettings();
		$this->request    =& $this->registry->fetchRequest();
		$this->lang       =  $this->registry->getClass('class_localization');
		$this->member     =  $this->registry->member();
		$this->memberData =& $this->registry->member()->fetchMemberData();
		$this->cache      =  $this->registry->cache();
		$this->caches     =& $this->registry->cache()->fetchCaches();
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	abstract public function adminForm();
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	abstract public function adminSave(array &$item_info, array $input);
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	abstract public function adminQuickSave(array &$item_info, $type);
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	abstract public function adminRemove();
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	abstract public function adminViewFileEdits();
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	abstract public function adminAjax();
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	abstract public function publicOnCreateItem(array &$invItem);
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 * If returning without a value, it is assumed to be true
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	abstract public function publicUseItem(array &$invItem);
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	abstract public function publicAjax(array &$invItem);
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	abstract public function publicOnResoldItem(array &$invItem);
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	abstract public function onItemRestock(array &$item);
	
	/**
	 * Get the description of the item, normally called when there is no intentory item being used
	 *
	 * @access	public
	 * @return	string
	 */
	public function parseStoreDescription()
	{
		return $this->item['item_description'];
	}
	
	/**
	 * Get the description of the item, normally called when there is one intentory items being used
	 *
	 * @access	public
	 * @param	array	$invItem	The inventory item information
	 * @return	string
	 */
	public function parseInventoryDescription(array $invItem)
	{
		return $this->item['item_description'];
	}
	
	/**
	 * Called when the inventory items are going to be removed from the database
	 *
	 * @access	public
	 * @param	array	$invItem	The inventory item
	 * @return	void
	 */
	public final function deleteItems(array $invItem)
	{
		$this->DB->delete('market_inventory',"i_id=".$invItem['i_id']);
	}
	
	/**
	 * Called when the inventory items are used with a message to store
	 *
	 * @access	public
	 * @param	array		$invItem		The inventory item
	 * @return	void
	 */
	public final function writeLog(array $invItem, $message = "")
	{
		$this->DB->insert('market_item_logs',array(
													'member_id' => $this->memberData['member_id'],
													'item_id' => $this->item['item_id'],
													'inv_id' => $invItem['i_id'],
													'ip_address' => $this->member->ip_address,
													'l_time' => IPS_UNIX_TIME_NOW,
													'log_message' => $message,
						)						);
	}
	
	/**
	 * Get the default name to item
	 *
	 * @access	protected
	 * @return	string
	 */
	public final function getDefaultName()
	{
		return $this->defaultName;
	}
	
	/**
	 * Get the default description to item
	 *
	 * @access	protected
	 * @return	string
	 */
	public final function getDefaultDescription()
	{
		return $this->defaultDescription;
	}
	
	/**
	 * Get the default description to item
	 *
	 * @access	protected
	 * @return	string
	 */
	public final function allowMultipleInstances()
	{
		return $this->allowMultiple;
	}
	
	/**
	 * Get the default description to item
	 *
	 * @access	protected
	 * @return	string
	 */
	public final function getDependcies()
	{
		return $this->dependcies;
	}
}
?>